package fi.helsinki.cs.ohtu10k.schedule;

import java.io.Serializable;
import org.joda.time.DateTime;
import org.joda.time.Duration;

/**
 * Class representing event data.
 * @author Jani Rönkä
 * @author Thomas Nyman
 *
 */
class PeriodEvent implements ScheduleEvent, Serializable {
	
	/**
	 * Serial version ID used for serialization. 
	 */
	private static final long serialVersionUID = 1565754179969797200L;
	
	private DateTime startDate;
	private Duration duration;
	private String name;
	private ScheduleCourse course;
	private boolean isNotifiable;
	
	/**
	 * Hidden parameterless constructor.
	 */
	@SuppressWarnings("unused")
	private PeriodEvent() {}
	
	PeriodEvent(DateTime startDate, Duration duration, 
		String name, ScheduleCourse course, 
		boolean isNotifiable) throws IllegalArgumentException {
		
		if( startDate == null || duration == null || name == null )
			throw new IllegalArgumentException();
		
		this.startDate = startDate;
		this.duration = duration;
		this.name = name;
		this.course = course;
		this.isNotifiable = isNotifiable;
	}

	/**
	 * Returns start date of event.
	 * @return Start date of event as Date.object.
	 */@Override
	public DateTime getStartDate() {
		return this.startDate;
	}
	
	/**
	 * Returns duration of event.
	 * @return	Duration of event as Duration object.
	 */
	@Override
	public Duration getDuration() {
		return this.duration;
	}

	/**
	 * Returns name of event.
	 * @return Name of event as String
	 */
	@Override
	public String getName() {
		return this.name;
	}

	/**
	 * Returns course event is assigned to.
	 * @return Course event is assigned to.
	 */
	@Override
	public ScheduleCourse getCourse() {
		return this.course;
	}

	/**
	 * Returns true if user should be notified of this event, otherwise false.
	 * @return True is user should be notified of this event, otherwise false.
	 */
	@Override
	public boolean isNotifiable() {
		return this.isNotifiable;
	}

	/**
	 * Compares event given as parameter to this event.
	 * @param se Event to be compare to.
	 * @return True if events are equal, false otherwise.
	 */
	public boolean equals(ScheduleEvent se) {
		return ( 
			this.startDate.equals( se.getStartDate() ) 
			&& this.duration.equals( se.getDuration() )
			&& this.name.equals( se.getName() ) 
			&& this.course.equals( se.getCourse() )
			&& this.isNotifiable == se.isNotifiable() 
		);
	}
	
	/**
	 * Compares event given as parameter to this event.
	 * @return 0 if events have equal startDates, <0 if compared
	 *  event takes place earlier, >0 otherwise.
	 */
	@Override
	public int compareTo(ScheduleEvent se) {
		return this.startDate.compareTo( se.getStartDate() );
	}
		
	@Override
	public String toString() {
		return getStartDate().getDayOfMonth() + "." + getStartDate().getMonthOfYear() + "." + getStartDate().getYear() +" " + (getCourse() != null ? getCourse().getAbbreviation() : "") + " "+ getName();
	}
	
}
